package com.yun.baymax.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.yun.baymax.R;
import com.yun.baymax.fragment.BillFragment;
import com.yun.baymax.fragment.BudgetFragment;
import com.yun.baymax.fragment.SavingFragment;

import java.util.ArrayList;
import java.util.List;

public class FinancialActivity extends AppCompatActivity {
    private List<Fragment> fragments = new ArrayList<>();
    private String[] titles = {"存款", "账单", "预算"};
    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_money);
//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);
//        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                finish();
//            }
//        });
        tabLayout = (TabLayout) findViewById(R.id.tab_bar);
        viewPager = (ViewPager) findViewById(R.id.view_pager);

        fragments.add(new SavingFragment());
        fragments.add(new BillFragment());
        fragments.add(new BudgetFragment());
        viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
        viewPager.setOffscreenPageLimit(2);
        tabLayout.setupWithViewPager(viewPager);
    }

    class PagerAdapter extends FragmentPagerAdapter {

        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
}
